package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.controller.request.StaffPageRequest;
import com.example.springboot.entity.Staff;
import com.example.springboot.service.IStaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin  //解决跨域错误
@RestController
@RequestMapping("/staff")
//是rest风格的staffcontroller
public class StaffController {

    @Autowired
    IStaffService staffService;

    //    StaffPageRequest传入用户名和电话信息
    @GetMapping("/page")
    public Result page(StaffPageRequest staffPageRequest){
        //        返回数据 Result.success传入一个对象
        return Result.success(staffService.page(staffPageRequest));

    }

//    post put get delete对应增改查删四类操作
    //    新增会员
    @PostMapping("/save")
    public Result save(@RequestBody Staff staff){
        staffService.save(staff);
        return Result.success();
    }

//    更新会员信息
    @PutMapping("/update")
    public Result update(@RequestBody Staff staff){
        staffService.update(staff);
        return Result.success();
    }

    //    获取用户列表
    @GetMapping("/list")
    public Result list(){
        //        返回数据
        List<Staff> staffs=staffService.list();
//        System.out.println("staffs内容是"+staffs);
        return Result.success(staffs);

    }


//    根据ID更新用户信息
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Staff staff =staffService.getById(id);
        return Result.success(staff);

    }

    //删除操作:
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        staffService.deleteById(id);
        return Result.success();
    }

    /*
    最初是通过List返回所有数据,而建立Result后采用Result返回数据
    @GetMapping("/list")
    public List<Staff> listStaffs(){
        return staffService.listStaffs();
    }
     */
}
